### NOTES:
#   The program allows for types with four projects, for future expansion.
#   Only observed types are considered (see paper for discussion).
###

# Lists of types by number of projects:

t0 <- which(l_t == 0 & !is.na(Y_t[,1]) )
t1 <- which(l_t == 1 & !is.na(Y_t[,1]) )
t2 <- which(l_t == 2 & !is.na(Y_t[,1]) )
t3 <- which(l_t == 3 & !is.na(Y_t[,1]) )
t4 <- which(l_t == 4 & !is.na(Y_t[,1]) )



### ADD ONE-AUTHOR PROJECT

# A) 0 Projects to 1 Projects

# Observed types t and t-bar:

temp0 <- cbind(types[t0,1], t0)
colnames(temp0) <- c("Z","t")

temp1 <- cbind(types[t1,1:2], t1)
colnames(temp1) <- c("Z","X1tb","tb")

# Merge by indiv. char's:

t0tb1.addX1 <- merge(temp0,temp1, by.x=c("Z"), by.y=c("Z") )

# Confirm added project has one author:

temp1 <- substr(levels(X_P_auth)[t0tb1.addX1$X1tb],1,1) == "1"

t0tb1.addX1[temp1,] -> t0tb1.addX1

###

# B) 1 Projects to 2 Projects

# Observed types t and t-bar:

temp1 <- cbind(types[t1,1:2], t1)
colnames(temp1) <- c("Z","X1t","t")

temp2 <- cbind(types[t2,1:3], t2)
colnames(temp2) <- c("Z","X1tb","X2tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t1tb2.addX1 <- merge(temp1,temp2, by.x=c("Z","X1t"), by.y=c("Z","X2tb") )
t1tb2.addX2 <- merge(temp1,temp2, by.x=c("Z","X1t"), by.y=c("Z","X1tb") )

# Confirm added project has one author:

temp1 <- substr(levels(X_P_auth)[t1tb2.addX1$X1tb],1,1) == "1"
temp2 <- substr(levels(X_P_auth)[t1tb2.addX2$X2tb],1,1) == "1"

t1tb2.addX1[temp1,] -> t1tb2.addX1
t1tb2.addX2[temp2,] -> t1tb2.addX2

###

# C) 2 Projects to 3 Projects

# Observed types t and t-bar:

temp2 <- cbind(types[t2,1:3], t2)
colnames(temp2) <- c("Z","X1t","X2t","t")

temp3 <- cbind(types[t3,1:4], t3)
colnames(temp3) <- c("Z","X1tb","X2tb","X3tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t2tb3.addX1 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X2tb","X3tb") )
t2tb3.addX2 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X1tb","X3tb") )
t2tb3.addX3 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X1tb","X2tb") )

# Confirm added project has one author:

temp1 <- substr(levels(X_P_auth)[t2tb3.addX1$X1tb],1,1) == "1"
temp2 <- substr(levels(X_P_auth)[t2tb3.addX2$X2tb],1,1) == "1"
temp3 <- substr(levels(X_P_auth)[t2tb3.addX3$X3tb],1,1) == "1"

t2tb3.addX1[temp1,] -> t2tb3.addX1
t2tb3.addX2[temp2,] -> t2tb3.addX2
t2tb3.addX3[temp3,] -> t2tb3.addX3

###

# D) 3 Projects to 4 Projects

# Observed types t and t-bar:

temp3 <- cbind(types[t3,1:4], t3)
colnames(temp3) <- c("Z","X1t","X2t","X3t","t")

temp4 <- cbind(types[t4,1:5], t4)
colnames(temp4) <- c("Z","X1tb","X2tb","X3tb","X4tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t3tb4.addX1 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X2tb","X3tb","X4tb") )
t3tb4.addX2 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X3tb","X4tb") )
t3tb4.addX3 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X2tb","X4tb") )
t3tb4.addX4 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X2tb","X3tb") )

# Confirm added project has one author:

temp1 <- substr(levels(X_P_auth)[t3tb4.addX1$X1tb],1,1) == "1"
temp2 <- substr(levels(X_P_auth)[t3tb4.addX2$X2tb],1,1) == "1"
temp3 <- substr(levels(X_P_auth)[t3tb4.addX3$X3tb],1,1) == "1"
temp4 <- substr(levels(X_P_auth)[t3tb4.addX4$X4tb],1,1) == "1"

t3tb4.addX1[temp1,] -> t3tb4.addX1
t3tb4.addX2[temp2,] -> t3tb4.addX2
t3tb4.addX3[temp3,] -> t3tb4.addX3
t3tb4.addX4[temp4,] -> t3tb4.addX4

###

# F) Combine

# Combine:

Add_1auth <- cbind(c(
	t0tb1.addX1$t,       
	t1tb2.addX1$t,       t1tb2.addX2$t,       
	t2tb3.addX1$t,       t2tb3.addX2$t,       t2tb3.addX3$t,       
	t3tb4.addX1$t,       t3tb4.addX2$t,       t3tb4.addX3$t,       t3tb4.addX4$t ),
	c(
	t0tb1.addX1$tb,       
	t1tb2.addX1$tb,       t1tb2.addX2$tb,       
	t2tb3.addX1$tb,       t2tb3.addX2$tb,       t2tb3.addX3$tb,       
	t3tb4.addX1$tb,       t3tb4.addX2$tb,       t3tb4.addX3$tb,       t3tb4.addX4$tb )
)

colnames(Add_1auth) <- c("t","tb")

# Remove duplicates:

Add_1auth <- unique(Add_1auth)



### ADD TWO-AUTHOR PROJECT

# A) 0 Projects to 1 Projects

# Observed types t and t-bar:

temp0 <- cbind(types[t0,1], t0)
colnames(temp0) <- c("Z","t")

temp1 <- cbind(types[t1,1:2], t1)
colnames(temp1) <- c("Z","X1tb","tb")

# Merge by indiv. char's:

t0tb1.addX1 <- merge(temp0,temp1, by.x=c("Z"), by.y=c("Z") )

# Confirm added project has two authors:

temp1 <- substr(levels(X_P_auth)[t0tb1.addX1$X1tb],1,1) == "2"

t0tb1.addX1[temp1,] -> t0tb1.addX1

###

# B) 1 Projects to 2 Projects

# Observed types t and t-bar:

temp1 <- cbind(types[t1,1:2], t1)
colnames(temp1) <- c("Z","X1t","t")

temp2 <- cbind(types[t2,1:3], t2)
colnames(temp2) <- c("Z","X1tb","X2tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t1tb2.addX1 <- merge(temp1,temp2, by.x=c("Z","X1t"), by.y=c("Z","X2tb") )
t1tb2.addX2 <- merge(temp1,temp2, by.x=c("Z","X1t"), by.y=c("Z","X1tb") )

# Confirm added project has two authors:

temp1 <- substr(levels(X_P_auth)[t1tb2.addX1$X1tb],1,1) == "2"
temp2 <- substr(levels(X_P_auth)[t1tb2.addX2$X2tb],1,1) == "2"

t1tb2.addX1[temp1,] -> t1tb2.addX1
t1tb2.addX2[temp2,] -> t1tb2.addX2

###

# C) 2 Projects to 3 Projects

# Observed types t and t-bar:

temp2 <- cbind(types[t2,1:3], t2)
colnames(temp2) <- c("Z","X1t","X2t","t")

temp3 <- cbind(types[t3,1:4], t3)
colnames(temp3) <- c("Z","X1tb","X2tb","X3tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t2tb3.addX1 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X2tb","X3tb") )
t2tb3.addX2 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X1tb","X3tb") )
t2tb3.addX3 <- merge(temp2,temp3, by.x=c("Z","X1t","X2t"), by.y=c("Z","X1tb","X2tb") )

# Confirm added project has two authors:

temp1 <- substr(levels(X_P_auth)[t2tb3.addX1$X1tb],1,1) == "2"
temp2 <- substr(levels(X_P_auth)[t2tb3.addX2$X2tb],1,1) == "2"
temp3 <- substr(levels(X_P_auth)[t2tb3.addX3$X3tb],1,1) == "2"

t2tb3.addX1[temp1,] -> t2tb3.addX1
t2tb3.addX2[temp2,] -> t2tb3.addX2
t2tb3.addX3[temp3,] -> t2tb3.addX3

###

# D) 3 Projects to 4 Projects

# Observed types t and t-bar:

temp3 <- cbind(types[t3,1:4], t3)
colnames(temp3) <- c("Z","X1t","X2t","X3t","t")

temp4 <- cbind(types[t4,1:5], t4)
colnames(temp4) <- c("Z","X1tb","X2tb","X3tb","X4tb","tb")

# Merge by indiv. and project char's (multiple combinations):

t3tb4.addX1 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X2tb","X3tb","X4tb") )
t3tb4.addX2 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X3tb","X4tb") )
t3tb4.addX3 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X2tb","X4tb") )
t3tb4.addX4 <- merge(temp3,temp4, by.x=c("Z","X1t","X2t","X3t"), by.y=c("Z","X1tb","X2tb","X3tb") )

# Confirm added project has two authors:

temp1 <- substr(levels(X_P_auth)[t3tb4.addX1$X1tb],1,1) == "2"
temp2 <- substr(levels(X_P_auth)[t3tb4.addX2$X2tb],1,1) == "2"
temp3 <- substr(levels(X_P_auth)[t3tb4.addX3$X3tb],1,1) == "2"
temp4 <- substr(levels(X_P_auth)[t3tb4.addX4$X4tb],1,1) == "2"

t3tb4.addX1[temp1,] -> t3tb4.addX1
t3tb4.addX2[temp2,] -> t3tb4.addX2
t3tb4.addX3[temp3,] -> t3tb4.addX3
t3tb4.addX4[temp4,] -> t3tb4.addX4

# F) Combine and Find Alter Types

# Combine:

t.addX <- cbind(c(
	t0tb1.addX1$t,       
	t1tb2.addX1$t,       t1tb2.addX2$t,       
	t2tb3.addX1$t,       t2tb3.addX2$t,       t2tb3.addX3$t,       
	t3tb4.addX1$t,       t3tb4.addX2$t,       t3tb4.addX3$t,       t3tb4.addX4$t ),
	c(
	t0tb1.addX1$X1tb,       
	t1tb2.addX1$X1tb,       t1tb2.addX2$X2tb,       
	t2tb3.addX1$X1tb,       t2tb3.addX2$X2tb,       t2tb3.addX3$X3tb,       
	t3tb4.addX1$X1tb,       t3tb4.addX2$X2tb,       t3tb4.addX3$X3tb,       t3tb4.addX4$X4tb ),
	c(
	t0tb1.addX1$tb,       
	t1tb2.addX1$tb,       t1tb2.addX2$tb,       
	t2tb3.addX1$tb,       t2tb3.addX2$tb,       t2tb3.addX3$tb,       
	t3tb4.addX1$tb,       t3tb4.addX2$tb,       t3tb4.addX3$tb,       t3tb4.addX4$tb )
)

colnames(t.addX) <- c("t","Xb","tb")

# Merge on added project X_P to find compatible alter types:

M.tsX <- merge(t.addX,t.addX, by="Xb")
names(M.tsX)
names(M.tsX) <- c("Xb","t","tb","s","sb")

# Check duplicates:

dim(M.tsX)
dim(unique(M.tsX))
dim(unique(M.tsX[,c("t","tb","s")]))

# Remove duplicates:

Add_2auth <- unique(M.tsX)

# Restrict to necessary columns (type indices):

Add_2auth <- Add_2auth[,c("t","tb","s")]



### CLEAN UP

rm(t0,t1,t2,t3,t4, temp0, temp1, temp2, temp3, temp4)
rm("t.addX", "t0tb1.addX1", "t1tb2.addX1", "t1tb2.addX2",
			 "t2tb3.addX1", "t2tb3.addX2", "t2tb3.addX3",
			 "t3tb4.addX1", "t3tb4.addX2", "t3tb4.addX3", "t3tb4.addX4")

